﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webui.Models;
using webui.ViewModel;

namespace webui.Areas.Admin.Controllers
{
    public class AdminController : Controller
    {

        [HttpGet]
        [Skip]
        public ActionResult Login()
        {

            return View();
        }


        [HttpPost]
        [Skip]
        public ActionResult Login(v_LoginUser userinfo)
        {

            if (!ModelState.IsValid)
            {
                return OperateContext.Current.RedirectAjax("err", "没有权限!", null, "");
            }

            if (OperateContext.Current.LoginAdmin(userinfo))
            {

                return OperateContext.Current.RedirectAjax("ok", "登陆成功~", null, "/admin/admin/index");
            }
            else
            {
                return OperateContext.Current.RedirectAjax("err", "用户名或密码错误~~!", null, "");
            }

        }


        [HttpGet]
        [Skip]
        public ActionResult Reguser()
        {

            return View();
        }


        //注册
        [HttpPost]
        [Skip]
        public ActionResult Reguser(v_RegUser reguser)
        {

            if (!ModelState.IsValid)
            {
                return OperateContext.Current.RedirectAjax("err", "输入不完整!", null, "");
            }
            if (reguser.LoginName.Length<6)
            {
                return OperateContext.Current.RedirectAjax("err", "登录名不能小于6位!", null, "");
            }
            if (reguser.Pwd!= reguser.Pwd2)
            {
                return OperateContext.Current.RedirectAjax("err", "两次密码不一致!", null, "");
            }


             UserInfoBLL iuserbll = new UserInfoBLL();
            if (iuserbll.GetSingle(s => s.loginname == reguser.LoginName) != null)
            {
                return OperateContext.Current.RedirectAjax("err", "用户已存在!", null, "");
            }


            UserInfo userM = new UserInfo();
            userM.addtime = DateTime.Now;
            userM.isDel = false;
            userM.pwd = Tools.Security.MD5(reguser.Pwd);
            userM.loginname = reguser.LoginName;
            userM.gender = true;

            RoleInfoBLL irolebll = new RoleInfoBLL();
            var rolem= irolebll.GetSingle(s=> s.rName=="普通用户");
            int uid = iuserbll.InsertReturnIdentity(userM);//添加账号
            
            UserRoleBLL iuserrolebll = new UserRoleBLL();
            UserRole userRoleM = new UserRole();
            userRoleM.isDel = false;
            userRoleM.uid = uid;
            userRoleM.rid = rolem.id;
            userRoleM.addtime = DateTime.Now;//添加角色
           bool isok =iuserrolebll.Insert(userRoleM);

            if (isok==false)
            {

                return OperateContext.Current.RedirectAjax("ok", "注册失败~", null, "/admin/admin/index");
            }

            v_LoginUser vlog = new v_LoginUser();
              vlog.LoginName = reguser.LoginName;
            vlog.Pwd = reguser.Pwd;
            if (OperateContext.Current.LoginAdmin(vlog))
            {
                return OperateContext.Current.RedirectAjax("ok", "登陆成功~", null, "/admin/admin/index");
            }
            else
            {
                return OperateContext.Current.RedirectAjax("err", "用户名或密码错误~~!", null, "");
            }

        }


        //[HttpPost]
        //[Skip]
        //public ActionResult RegUser(v_RegUser userinfo)
        //{

        //    if (!ModelState.IsValid)
        //    {
        //        return OperateContext.Current.RedirectAjax("err", "没有权限!", null, "");
        //    }

        //    if (OperateContext.Current.LoginAdmin(userinfo))
        //    {

        //        return OperateContext.Current.RedirectAjax("ok", "登陆成功~", null, "/admin/admin/index");
        //    }
        //    else
        //    {
        //        return OperateContext.Current.RedirectAjax("err", "用户名或密码错误~~!", null, "");
        //    }

        //}



        [SkipPemission]
        public ActionResult Index()
        {
            ViewBag.username = OperateContext.Current.Usr.loginname;
            return View();
        }

        [Skip]
        public ActionResult Quit()
        {
            Session["ainfo"] = null;
            Session.Clear();
            HttpCookie myCookie = Request.Cookies["ainfo"];
            if (myCookie != null)
            {
                myCookie.Path = "/admin/";
                myCookie.Expires = DateTime.Today.AddDays(-1);
                Response.Cookies.Add(myCookie);
            }

            return Redirect("login");

        }


       
        //  根据当前登陆用户 权限  生成菜单 
        [AjaxRequest]
        [SkipPemission]
        public ActionResult GetMenuData()
        {
            return Content(OperateContext.Current.UsrTreeJsonStr);
        }


    }
}